import tkinter as tk
from tkinter import ttk
from ui.DataPreprocessingPage import DataPreprocessingPage
from ui.DataLoadingPage import DataLoadingPage
from ui.ModelSettingPage import ModelSettingPage
from ui.ModelTrainingPage import ModelTrainingPage
from ui.FaultDiagnosisPage import FaultDiagnosisPage
from ui.LifePredictionPage import LifePredictionPage
from ui.ResultAnalysisPage import ResultAnalysisPage

class App(tk.Tk):
    """主应用类，作为全局控制器"""
    def __init__(self):
        super().__init__()
        self.title("输送机轴承健康管理系统")
        self.geometry("800x600")
        self.current_page = ""  # 新增当前页面状态跟踪
        # 创建菜单栏
        self.create_menu()
        
        # 创建主框架
        self.main_frame = ttk.Frame(self)
        self.main_frame.pack(fill=tk.BOTH, expand=True)
        
        # 存储所有页面的字典
        self.pages = {}
        
        # 创建所有页面
        self.create_pages()
        
        # 显示首页
        self.show_page("DataLoadingPage")
    
    def create_menu(self):
        """创建带选中状态的菜单栏"""
        menubar = tk.Menu(self)
        
        # 带选中标记的菜单项
        menu_items = [
            ("数据加载", "DataLoadingPage"),
            ("数据预处理", "DataPreprocessingPage"),
            ("模型设置", "ModelSettingPage"),
            ("模型训练", "ModelTrainingPage"),
            ("故障诊断", "FaultDiagnosisPage"),
            ("寿命预测", "LifePredictionPage"),
            ("结果分析", "ResultAnalysisPage"),
        ]
        
        for text, page_name in menu_items:
            label = f"✓ {text}" if self.current_page == page_name else text
            menubar.add_command(
                label=label,
                command=lambda p=page_name: self.show_page(p),
                foreground="#2c7be5" if self.current_page == page_name else "#000000"
            )
            menubar.add_separator()
        
        self.config(menu=menubar)

    def show_page(self, page_name):
        """显示指定页面并更新菜单状态"""
        self.current_page = page_name  # 更新当前页面状态
        # 隐藏所有页面
        for page in self.pages.values():
            page.frame.pack_forget()
        
        # 显示选中的页面
        self.pages[page_name].frame.pack(fill=tk.BOTH, expand=True)
        self.create_menu()  # 刷新菜单状态

    def create_pages(self):
        """创建所有功能页面"""
        self.pages["DataLoadingPage"] = DataLoadingPage(self.main_frame, self)
        self.pages["DataPreprocessingPage"] = DataPreprocessingPage(self.main_frame, self)
        self.pages["ModelSettingPage"] = ModelSettingPage(self.main_frame, self)
        self.pages["ModelTrainingPage"] = ModelTrainingPage(self.main_frame, self)
        self.pages["FaultDiagnosisPage"] = FaultDiagnosisPage(self.main_frame, self)
        self.pages["LifePredictionPage"] = LifePredictionPage(self.main_frame, self)
        self.pages["ResultAnalysisPage"] = ResultAnalysisPage(self.main_frame, self)